package com.jie.deletefile.Gongju;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jie.deletefile.Gongju.Entity.AppInfo;
import com.jie.deletefile.Gongju.comment.AppInfoManager;
import com.jie.deletefile.R;

import java.util.List;

public class AppActivity extends AppCompatActivity implements View.OnClickListener {

    private Button a_all;
    private Button a_user;
    private Button a_system;
    private ListView a_lv;
    private MyAdapter adapter;
    private AppInfoManager manager;
    private String TAG = "appActivity";
    private List<AppInfo> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        initView();
        manager = AppInfoManager.getAppInfoManager(this);
        data = manager.getUserPackageInfo(false);
        adapter = new MyAdapter(this, R.layout.item_city,data);
        a_lv.setAdapter(adapter);
    }

    private void initView() {
        a_all = (Button) findViewById(R.id.a_all);
        a_user = (Button) findViewById(R.id.a_user);
        a_system = (Button) findViewById(R.id.a_system);
        a_lv = (ListView) findViewById(R.id.a_lv);

        a_all.setOnClickListener(this);
        a_user.setOnClickListener(this);
        a_system.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.a_all:
                data = manager.getAllPackageInfo(false);
                adapter.notifyDataSetChanged();
                break;
            case R.id.a_user:
                Log.e(TAG, "onClick: " + manager.getUserPackageInfo(true).size() + "    " + manager.getUserPackageInfo(true).get(0).toString());
                data = manager.getUserPackageInfo(false);
                adapter.notifyDataSetChanged();
                break;
            case R.id.a_system:
                data = manager.getSystemPackageInfo(false);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    class MyAdapter extends ArrayAdapter{

        private List<AppInfo> data1;
        private int resID;
        private Context context;

        public MyAdapter(Context context, int resource, List<AppInfo> data1 ) {
            super(context, resource);
            this.context = context;
            this.data1 = data1;
            resID = resource;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(resID, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.item_tv);
                tv.setText(data1.get(position).getPackageInfo().packageName.toString());
                Log.e(TAG, "getView: "+data1.get(position).getPackageInfo().packageName);
            return convertView;

        }
    }
}
